package com.example.administrator.baseproject;

import android.support.annotation.LayoutRes;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import baseinterface.NetChangeDeal;
import butterknife.ButterKnife;
import constants.Constants;
import constants.HandlerConstant;
import utils.ActivityUtil;
import utils.LoadingUtils;
import utils.ToastUtil;

public class BaseFragmentActivity extends RootFragmentActivity implements NetChangeDeal{

    public static final String TAG = "BaseActivity";
    //网络监听的回调对象
    public static NetChangeDeal netChangeDeal;
    public View loadingView, emptyView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        netChangeDeal = this;
        ButterKnife.bind(this);
        initView();
        setListener();
    }


    @Override
    protected void onStart() {
        super.onStart();
        loadingTask();
    }

    protected void initView() {
    }

    protected void setListener() {
    }

    public void showLoadingView(ViewGroup viewGroup) {
        if (loadingView == null) {
            loadingView = LoadingUtils.showLoadingView(viewGroup,BaseFragmentActivity.this, "正在加载");
        }
    }

    protected void showLoadingView(ViewGroup viewGroup,String msg) {
        if (loadingView == null) {
            loadingView = LoadingUtils.showLoadingView(viewGroup,BaseFragmentActivity.this, msg);
        }
    }

    protected void hideLoadingView() {
        LoadingUtils.hideLoadingView(loadingView);
        loadingView = null;
    }

    protected void showEmptyView(ViewGroup viewGroup) {
        if (emptyView == null) {
            emptyView = LoadingUtils.showEmptyView(viewGroup,BaseFragmentActivity.this, "暂无数据",R.mipmap.ic_launcher);
        }
    }

    protected void showEmptyView(ViewGroup viewGroup,String msg,int imgRes) {
        if (emptyView == null) {
            emptyView = LoadingUtils.showEmptyView(viewGroup,BaseFragmentActivity.this, msg,imgRes);
        }
    }

    protected void hideEmptyView() {
        LoadingUtils.hideEmptyView(emptyView);
        emptyView = null;
    }

    protected void loadingTask() {
    }

    @Override
    public void onNetChange(int netState) {
        if (netState == Constants.NETWORK_NONE) {
            sendEmptyMessage(HandlerConstant.NETWORK_NONE);
            ToastUtil.showToast(this, "没有网络连接");
        } else if (netState == Constants.NETWORK_MOBILE) {
            sendEmptyMessage(HandlerConstant.NETWORK_MOBILE);
            ToastUtil.showToast(this, "移动网络已连接");
        } else if (netState == Constants.NETWORK_WIFI) {
            sendEmptyMessage(HandlerConstant.NETWORK_WIFI);
            ToastUtil.showToast(this, "WIFI已连接");
        } else if (netState == Constants.NETWORK_ETHERNET) {
            sendEmptyMessage(HandlerConstant.NETWORK_ETHERNET);
            ToastUtil.showToast(this, "以太网已连接");
        }
    }

}
